package com.artqiyi.dollmachine.utils.manager;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.artqiyi.dollmachine.common.Base.BaseApp;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.common.constant.ServerConstant;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.Callback.MyStringCallback;
import com.artqiyi.dollmachine.utils.JSONUtils;
import com.artqiyi.dollmachine.utils.NetworkUtil;
import com.artqiyi.dollmachine.utils.respone.GameRecordsListRespone;
import com.artqiyi.dollmachine.utils.respone.UserInfoRespone;
import com.artqiyi.dollmachine.views.activity.MainActivity;
import com.artqiyi.dollmachine.wxapi.WXEntryActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/13 0013
 * desc:
 */

public class HttpManager {

    public static void getHttpRequest(String url, Map<String, String> params, Callback<String> callback) {
        if(!NetworkUtil.isConnected(BaseApp.getInstance())) {
            callback.onError(null, new Exception("网络请求失败，请检查您的网络"), 0);
            return;
        }
        Timber.d("url:" + url);
        Timber.d("get_Api_token:" + UserInfoManager.getInstance().getUserInfoBean().getApi_token());
        OkHttpUtils
                .get()
                .url(url)
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", TextUtils.isEmpty(UserInfoManager.getInstance().getUserInfoBean().getApi_token())? "": UserInfoManager.getInstance().getUserInfoBean().getApi_token())
                .addHeader("X-App-Client", "android")
                .addHeader("X-Product-Version", Constant.X_PRODUCT_VERSION)
                .params(params==null? new HashMap<String, String>(): params)
                .build()
                .execute(callback);
    }

    public static void postHttpRequest(String url, Map<String, String> params, Callback<String> callback) {
        if(!NetworkUtil.isConnected(BaseApp.getInstance())) {
            callback.onError(null, new Exception("网络请求失败，请检查您的网络"), 0);
            return;
        }
        Timber.d("url:" + url);
        Timber.d("Api_token:" + UserInfoManager.getInstance().getUserInfoBean().getApi_token());
        OkHttpUtils
                .post()
                .url(url)
				.addHeader("Authorization", TextUtils.isEmpty(UserInfoManager.getInstance().getUserInfoBean().getApi_token())? "": UserInfoManager.getInstance().getUserInfoBean().getApi_token())
                .addHeader("Accept", "application/json")
                .addHeader("X-App-Client", "android")
                .addHeader("X-Product-Version", Constant.X_PRODUCT_VERSION)
                .params(params==null? new HashMap<String, String>(): params)
                .build()
                .execute(callback);
    }
}
